package com.xinxi11.project.qx.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.xinxi11.module.core.dao.Finder;
import com.xinxi11.module.core.service.BaseServiceImpl;
import com.xinxi11.project.qx.beans.QxRole;
import com.xinxi11.project.qx.services.IQxRoleService;

@Service("qxRoleService")
public class QxRoleServiceImpl extends BaseServiceImpl implements IQxRoleService {

	@Override
	public QxRole findByName(String roleName) {
		if (StringUtils.isBlank(roleName))
			return null;
		String sql = " from QxRole where active=0 and name=:roleName ";
		Finder finder = new Finder(sql);
		finder.setParam("roleName", roleName);
		return baseDao.queryForObject(finder);
	}

	@Override
	public List<QxRole> findLikeName(String roleName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public QxRole findMaxRole(String parentId) {
		// 无须判断角色是否有效
		String hql = " from QxRole where id =( select max(id) from QxRole where  parentId=:parentId)  ";
		Finder finder = new Finder(hql);
		finder.setParam("parentId", parentId);
		return baseDao.queryForObject(finder);
	}

	// 查询角色包含父类名称
	@Override
	public QxRole findRoleById(String roleId) {
		// 查询角色信息
		QxRole role = baseDao.findById(QxRole.class, roleId);
		if (role != null) {
			if (StringUtils.isNotBlank(role.getParentId())) {
				QxRole parentRole = baseDao.findById(QxRole.class, role.getParentId());
				role.setParentRoleName(parentRole.getName());
			}
		}
		return role;
	}

	@Override
	public List<QxRole> findRoleByUser(Long userId) {
		List<String> roleIds = findRoleIds(findDirectRoleIds(userId));
		List<QxRole> listRoles = findRolesByIds(roleIds);
		return listRoles;
	}

	@Override
	public List<QxRole> findRolesByUserRole(String roleId) {
		List<String> lst = new ArrayList<String>();
		lst.add(roleId);
		List<String> roleIds = findRoleIds(lst);
		List<QxRole> listRoles = findRolesByIds(roleIds);
		return listRoles;
	}

	// 查找用户直接角色（有父子和孙子关系的去除）
	public List<QxRole> findDirectRoles(Long userId) {
		return findRolesByIds(findDirectRoleIds(userId));
	}

	// 查找用户直接角色（有父子和孙子关系的去除）
	@SuppressWarnings("unchecked")
	public List<String> findDirectRoleIds(Long userId) {
		// 查询用户直接拥有的角色Ids
		String sql = " select r.id from QxRole as r,QxreUserRole  as reur  where r.active=0 and reur.roleId=r.id and reur.userId=:userId order by r.level ";
		Finder finder = new Finder(sql);
		finder.setParam("userId", userId);
		List<String> roleIds = baseDao.queryForList(finder, null);
		// 查询这个人的岗位对应的角色
		String hql_dutyRole = " select dr.roleId from QxreDutyRole as dr,ReUserDuty as ud where dr.active=0  and dr.dutyId=ud.duty.id  and ud.user.id=:userId ";
		Finder finder_dutyRole = new Finder(hql_dutyRole);
		finder_dutyRole.setParam("userId", userId);
		List<String> listDutyRoleId = baseDao.queryForList(finder_dutyRole, null);
		// 与角色岗位取并集
		roleIds = (List<String>) CollectionUtils.union(listDutyRoleId, roleIds);
		if (roleIds == null || roleIds.size() == 0)
			return null;
		// 遍历角色Ids，去除有子孙关系的Ids
		List<String> listIdDel = new ArrayList<String>();
		for (String id : roleIds) {
			for (String id2 : roleIds) {
				if ((!id2.equals(id)) && id2.startsWith(id)) {
					listIdDel.add(id2);
				}
			}
		}
		roleIds = (List<String>) CollectionUtils.subtract(roleIds, listIdDel);
		return roleIds;
	}

	// 查询ids查询roles
	public List<QxRole> findRolesByIds(List<String> ids) {
		String sql_role = " from QxRole where active=0 and id in (:roleIds) ";
		Finder finder_role = new Finder(sql_role);
		finder_role.setParam("roleIds", ids);
		List<QxRole> roles = baseDao.queryForList(finder_role, null);
		return roles;
	}

	// 查询子roles
	@SuppressWarnings("unchecked")
	public List<String> findChildRoles(List<String> parentIds) {
		String sql = " select id from QxRole where active=0 and parentId in (:parentIds)";
		Finder finder = new Finder(sql);
		finder.setParam("parentIds", parentIds);
		List<String> roleIds = baseDao.queryForList(finder, null);
		if (roleIds != null && roleIds.size() > 0) {
			List<String> listId = findRoleIds(roleIds);
			if (listId != null && listId.size() > 0) {
				roleIds = (List<String>) CollectionUtils.union(roleIds, listId);
			}
		}
		return roleIds;
	}

	@SuppressWarnings("unchecked")
	public List<String> findRoleIds(List<String> ids) {
		if (ids != null && ids.size() > 0) {
			List<String> listId = findChildRoles(ids);
			if (listId != null && listId.size() > 0) {
				ids = (List<String>) CollectionUtils.union(ids, listId);
			}
		}
		return ids;

	}

	@Override
	public List<QxRole> findAllRole() {
		String hql = "from QxRole where active=0";
		Finder finder = new Finder(hql);
		List<QxRole> roles = baseDao.queryForList(finder, null);
		return roles;
	}
}
